package com.ssh.nio.channel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;

public class GatheringChallelTest {
	public static void main(String[] args) throws IOException {
		// ByteBuffer[] buffers = new ByteBuffer[3];
		// List list = new ArrayList();

		// list.toArray(buffers);

		FileOutputStream out = new FileOutputStream("G:\\a.txt");
		//
		// out.write(new String("hello").getBytes());
		//

		// 支持多缓冲区的写通道
		GatheringByteChannel gatherChannel = out.getChannel();
		ByteBuffer[] buffers = getbuffers();
		while (gatherChannel.write(buffers) > 0) {
			System.out.println("-----write-----");
		}
		out.close();

	}

	private static ByteBuffer[] getbuffers() {
		// TODO Auto-generated method stub
		ByteBuffer[] buffers = new ByteBuffer[3];

		for (int i = 0; i < buffers.length; i++) {
			buffers[i] = ByteBuffer.allocate(220);
			buffers[i].put(("myfirstniobuffer_index" + i).getBytes());
			// 翻转一次表示准备读取.否则读出去的是乱码
			buffers[i].flip();
		}

		return buffers;
	}
}
